//给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。 
//
// 百度百科中最近公共祖先的定义为：“对于有根树 T 的两个节点 p、q，最近公共祖先表示为一个节点 x，满足 x 是 p、q 的祖先且 x 的深度尽可能大（
//一个节点也可以是它自己的祖先）。” 
//
// 
//
// 示例 1： 
//
// 
//输入：root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
//输出：3
//解释：节点 5 和节点 1 的最近公共祖先是节点 3 。
// 
//
// 示例 2： 
//
// 
//输入：root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
//输出：5
//解释：节点 5 和节点 4 的最近公共祖先是节点 5 。因为根据定义最近公共祖先节点可以为节点本身。
// 
//
// 示例 3： 
//
// 
//输入：root = [1,2], p = 1, q = 2
//输出：1
// 
//
// 
//
// 提示： 
//
// 
// 树中节点数目在范围 [2, 10⁵] 内。 
// -10⁹ <= Node.val <= 10⁹ 
// 所有 Node.val 互不相同 。 
// p != q 
// p 和 q 均存在于给定的二叉树中。 
// 
// Related Topics 树 深度优先搜索 二叉树 👍 1638 👎 0

package leetcode.editor.cn;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class _236_LowestCommonAncestorOfABinaryTree {

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    public static void main(String[] args) {
        Solution solution = new _236_LowestCommonAncestorOfABinaryTree().new Solution();
        TreeNode root = new TreeNode(3);
        root.left = new TreeNode(5);
            root.left.left = new TreeNode(6);
            root.left.right = new TreeNode(2);
                root.left.right.left = new TreeNode(7);
                root.left.right.right = new TreeNode(4);
        root.right = new TreeNode(1);
            root.right.left = new TreeNode(0);
            root.right.right = new TreeNode(8);

//        System.out.println(solution.lowestCommonAncestor(root, root.left, root.right).val);
        System.out.println(solution.lowestCommonAncestor(root, root.left, root.left.right.right).val);

    }

    class Solution {
        Map<TreeNode, TreeNode> map;
        Set<TreeNode> set;

        public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
            map = new HashMap<>();
            dfs(root);
            set = new HashSet<>();
            TreeNode cur = p;
            while (cur != root) {
                set.add(cur);
                cur = map.get(cur);
            }
            set.add(root);
            cur = q;
            while (cur != root) {
                if (set.contains(cur)) {
                    return cur;
                } else {
                    cur = map.get(cur);
                }
            }
            return root;
        }

        private void dfs(TreeNode root) {
            if (root.left != null) {
                map.put(root.left, root);
                dfs(root.left);
            }
            if (root.right != null) {
                map.put(root.right, root);
                dfs(root.right);
            }
        }
    }

    // 官方给的递归，漂亮多了
    class Solution2 {
        private TreeNode res;

        public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
            res = null;
            dfs(root, p, q);
            return res;
        }

        private boolean dfs(TreeNode root, TreeNode p, TreeNode q) {
            if (root == null) return false;

            boolean lson = dfs(root.left, p, q);
            boolean rson = dfs(root.right, p, q);
            if ((lson && rson) || ((root == p || root == q) && (lson || rson))) {
                res = root;
            }
            return lson || rson || (root == p || root == q);
        }
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     * int val;
     * TreeNode left;
     * TreeNode right;
     * TreeNode(int x) { val = x; }
     * }
     */
    // 自己写的递归套递归，丑的很
    class Solution1 {
        public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
            boolean leftP = dfs(root.left, p);
            boolean leftQ = dfs(root.left, q);
            boolean rightP = dfs(root.right, p);
            boolean rightQ = dfs(root.right, q);
            if ((leftP && rightQ) || (leftQ && rightP) || root == p || root == q) {
                return root;
            } else if (leftP) {
                return lowestCommonAncestor(root.left, p, q);
            } else {
                return lowestCommonAncestor(root.right, p, q);
            }
        }

        private boolean dfs(TreeNode root, TreeNode node) {
            if (root == null) return false;
            if (root == node) return true;

            return dfs(root.left, node) || dfs(root.right, node);
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}